package com.gutai.admin.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import com.gutai.admin.dao.IAttributeCategoryDao;
import com.gutai.admin.entity.AttributeCategory;
import com.gutai.admin.service.IAttributeCategoryService;

/**
 * 
 * 描述：
 * 
 * <pre>
 * HISTORY
 * ****************************************************************************
 *  ID    DATE            PERSON            REASON
 *  1     2018年11月7日         YC           Create
 * ****************************************************************************
 * </pre>
 * 
 * @author YC
 * @version 1.0
 */
@Service
@CacheConfig(cacheNames = "attributeCategory")
public class AttributeCategoryService implements IAttributeCategoryService {

    @Autowired
    private IAttributeCategoryDao attributeCategoryDao;
    @Override
    public List<AttributeCategory> getList(String name) {
//        return attributeCategoryDao.findByName(name);
        return getAll().stream().filter(item->item.getName().indexOf(name)>=0).collect(Collectors.toList());
    }

    @Override
    @Cacheable(unless = "#result == null || #result.size()==0")
    public List<AttributeCategory> getAll() {
        return attributeCategoryDao.findAll();
    }

    
    @Override
    @CacheEvict(allEntries = true)
    public void add(AttributeCategory entity) {
        attributeCategoryDao.save(entity);
    }

    @Override
    @CacheEvict(allEntries = true)
    public void update(AttributeCategory entity) {
        attributeCategoryDao.saveAndFlush(entity);
    }

    @Override
    @CacheEvict(allEntries = true)
    public void delete(int id) {
        attributeCategoryDao.deleteById(id);
    }

    @Override
    public AttributeCategory findById(int id) {
        return attributeCategoryDao.findById(id);
    }
}
